package com.smx.dao;

import com.alibaba.druid.pool.DruidDataSource;
import com.smx.model.entity.User;
import com.smx.model.vo.UserVo;
import com.smx.util.DruidUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @ClassName UserDao
 * @Description TODO
 * @Author moses
 * @Date 2021/5/9
 **/
public class UserDao {

    private static final String SQL_UPDATEUSER = "update user set name=?,account=?,password=?,email=?,gender=?,avatar=?,remarks=?,phone=? where id=?";
    private static final String SQL_FINDUSER = "select * from user where account=? and password =?";
    private static final String sQL_FINDUSERBYID = "select * from user where id=?";


    public static User findUser(User user) {
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet rs = null;
        try {
            state = conn.prepareStatement(SQL_FINDUSER);
            state.setString(1, user.getAccount());
            state.setString(2, user.getPassword());
            rs=state.executeQuery();
            if (rs.next()) {
                UserVo userVo = new UserVo();
                userVo.setId(rs.getInt("id"));
                userVo.setName(rs.getString("name"));
                userVo.setAccount(rs.getString("account"));
                userVo.setPassword(rs.getString("password"));
                userVo.setEmail(rs.getString("email"));
                userVo.setGender(rs.getInt("gender"));
                userVo.setAvatar(rs.getString("avatar"));
                userVo.setRemarks(rs.getString("remarks"));
                userVo.setPhone(rs.getString("phone"));
                return userVo;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DruidUtil.close(conn,state,rs);
        }
        return null;
    }

    public static int updateUser(User user) {
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        try {
            state = conn.prepareStatement(SQL_UPDATEUSER);
            state.setInt(9, user.getId());
            state.setString(1, user.getName());
            state.setString(2,user.getAccount());
            state.setString(3,user.getPassword());
            state.setString(4,user.getEmail());
            state.setInt(5,user.getGender());
            state.setString(6,user.getAvatar());
            state.setString(7,user.getRemarks());
            state.setString(8,user.getPhone());
            return state.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DruidUtil.close(conn,state,null);
        }
        return 0;
    }

    public static User findUserById(int id) {
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet rs = null;
        try {
            state = conn.prepareStatement(sQL_FINDUSERBYID);
            state.setInt(1, id);
            rs  = state.executeQuery();
            if (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("name"));
                user.setAccount(rs.getString("account"));
                user.setPassword(rs.getString("password"));
                user.setEmail(rs.getString("email"));
                user.setGender(rs.getInt("gender"));
                user.setAvatar(rs.getString("avatar"));
                user.setRemarks(rs.getString("remarks"));
                user.setPhone(rs.getString("phone"));
                return user;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DruidUtil.close(conn,state,rs);
        }
        return null;

    }

}
